<?php
/**
 *   This file is part of ColumnBreak (a Joomla plugin).
 *   Written by Ivar Bergman (www.ivarbergman.se)
 *   Copyright (c) 2010 Ivar Bergman. All rights reserved.
 *
 *   ColumnBreak is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   ColumnBreak is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with ColumnBreak.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

/**
 * Editor Readmore buton
 *
 * @package Editors-xtd
 * @since 1.5
 */
class plgContentColumnBreak extends JPlugin
{
	/**
	 * Constructor
	 *
	 * For php4 compatability we must not use the __constructor as a constructor for plugins
	 * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
	 * This causes problems with cross-referencing necessary for the observer design pattern.
	 *
	 * @param 	object $subject The object to observe
	 * @param 	array  $config  An array that holds the plugin configuration
	 * @since 1.5
	 */
	function plgContentColumnBreak(& $subject, $config)
	{
  
		parent::__construct($subject, $config);
	}

        function onBeforeContentSave( &$article, $isNew )
        {
	  global $mainframe;

	  if (preg_match("/system-column-break/",$article->introtext))
	    {
	      
	      $mm_open  = "<!--START-SYSTEM-COLUMN-BREAK--><div class='system-column-break'><!--END-SYSTEM-COLUMN-BREAK-->";
	      $mm_close = "<!--START-SYSTEM-COLUMN-BREAK--></div><!--END-SYSTEM-COLUMN-BREAK-->";
	      $article->introtext = "{$mm_open}{$article->introtext}{$mm_close}";
	      $article->introtext = preg_replace("/<hr class=['\"]system-column-break['\"]\s*\/>/", "{$mm_close}<hr class=\"system-column-break\" />{$mm_open}", $article->introtext);
		     
	    }
	  else
	    {
	      $article->introtext = preg_replace("/<!--START-SYSTEM-COLUMN-BREAK-->[^!]*<!--END-SYSTEM-COLUMN-BREAK-->/", "", $article->introtext);
	    }


	  return true;
        }
}